//
//  command.h
//  usb2wireless
//
//  Created by alivehex on 14-10-27.
//
//

#ifndef __usb2wireless__command__
#define __usb2wireless__command__

#include <stdio.h>
#include "cbuf.h"

#define MAX_DATA_LEN		(80)
#define TIMEOUT_ROUNDS		(0x1FFF)
#define SYNC_HEADER0		('!')
#define SYNC_HEADER1		('$')

// u2w device request
typedef enum {
	U2WRQ_RADIO_WRITE = 0,	// write data to radio
	U2WRQ_RADIO_READ,		// read data from radio buffer
	U2WRQ_MEM8_RW,
	U2WRQ_MEM8_BIT,
	U2WRQ_MCU_RESET,
	U2WRQ_FLASH_WRITE,
	U2WRQ_FLASH_READ,
	U2WRQ_EEPROM_WRITE,
	U2WRQ_EEPROM_READ,
	U2WRQ_IR_CONFIG,
	U2WRQ_IR_WDATA,
	U2WRQ_LAST_ERR,
	U2WRQ_READ_TEMP,
	U2WRQ_IDLE,
	U2WRQ_ILLEGAL,
} u2w_request_t;

typedef enum {
	PROC_STATE_SYNC0 = 0,
	PROC_STATE_SYNC1,
	PROC_STATE_INDEX,
	PROC_STATE_NOD,
	PROC_STATE_DATA,
	PROC_STATE_CRC,
} proc_state_t;

typedef struct {
	int(*readInput)(char *buffer, int len);
	int(*writeOutput)(const char *buffer, int len);
	proc_state_t state;
	uint8_t cmdIndex;
	uint8_t	cmdDataLen;
	uint8_t cmdData[MAX_DATA_LEN];
	uint8_t	crcValue;
	uint8_t cmdDataIndex;
	uint32_t timeout;
} proc_package_t;

typedef union {
	uint16_t word;
	uint8_t bytes[2];
} cmd_word_t;


void processCommandStream(proc_package_t *pkg);

#endif /* defined(__usb2wireless__command__) */
